package cn.wenhe9.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wenhe9.common.utils.ResultResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wenhe9.gulimall.product.entity.AttrGroupEntity;
import cn.wenhe9.gulimall.product.service.AttrGroupService;

import javax.annotation.Resource;


/**
 * 属性分组
 *
 * @author dujinliang
 * @email dujinliang9@163.com
 * @date 2022-04-14 19:17:08
 */
@Api(tags = "属性分组管理接口")
@RequestMapping("/api/product/attrgroup")
@RestController
public class AttrGroupController {

    @Resource
    private AttrGroupService attrGroupService;

    /**
     * 获取属性分组列表
     * @param catId Long
     * @param page Long
     * @param limit Long
     * @param key Long
     * @return ResultResponse
     */
    @ApiOperation("获取属性分组列表")
    @GetMapping("/list/{catId}")
    public ResultResponse list(
            @PathVariable(name = "catId") Long catId,
            @RequestParam(defaultValue = "0") Long page,
            @RequestParam(defaultValue = "10") Long limit,
            @RequestParam(defaultValue = "") String key
    ){
        if (catId == 0){
            Page<AttrGroupEntity> groupPage = attrGroupService.lambdaQuery().like(AttrGroupEntity::getAttrGroupName, key).page(new Page<>(page, limit));
            return ResultResponse.ok(groupPage);
        }else {
            Page<AttrGroupEntity> groupPage = attrGroupService.lambdaQuery().eq(AttrGroupEntity::getCatelogId, catId).like(AttrGroupEntity::getAttrGroupName, key).page(new Page<>(page, limit));
            return ResultResponse.ok(groupPage);
        }
    }

    /**
     * 新增属性分组
     * @param attrGroup PmsAttrGroup
     * @return ResultResponse
     */
    @ApiOperation("新增属性分组")
    @PostMapping("/save")
    public ResultResponse save(@RequestBody AttrGroupEntity attrGroup){
        attrGroupService.save(attrGroup);
        return ResultResponse.ok();
    }

    /**
     * 更新属性分组
     * @param attrGroup PmsAttrGroup
     * @return ResultResponse
     */
    @ApiOperation("更新属性分组")
    @PostMapping("/update")
    public ResultResponse update(@RequestBody AttrGroupEntity attrGroup){
        attrGroupService.updateById(attrGroup);
        return ResultResponse.ok();
    }

    /**
     * 批量删除属性分组
     * @param ids List<Long>
     * @return ResultResponse
     */
    @ApiOperation("批量删除属性分组")
    @DeleteMapping("/delete")
    public ResultResponse deleteBatch(@RequestBody List<Long> ids){
        attrGroupService.deleteBatch(ids);
        return ResultResponse.ok();
    }

    /**
     * 查询属性分组详细信息
     * @param groupId Long
     * @return ResultResponse
     */
    @ApiOperation("查询属性分组详细信息")
    @GetMapping("/info/{groupId}")
    public ResultResponse info(@PathVariable(name = "groupId") Long groupId){
        AttrGroupEntity attrGroup = attrGroupService.getById(groupId);
        return ResultResponse.ok(attrGroup);
    }
}

